package com.tanhua.controller;

import com.tanhua.domain.Settings;
import com.tanhua.domain.UserInfo;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.service.BlackListService;
import com.tanhua.service.QuestionService;
import com.tanhua.service.SettingsService;
import com.tanhua.service.UserInfoService;
import com.tanhua.util.JWTUtil;
import com.tanhua.vo.BlackListVo;
import com.tanhua.vo.QuestionVo;
import com.tanhua.vo.SettingsVo;
import com.tanhua.vo.UserInfoVo;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/users")
public class UsersController {


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private BlackListService blackListService;

    /**
     *  查找当前登录用户的详细信息
     * @param request
     * @return
     */
    @GetMapping
    public ResponseEntity<UserInfoVo> findById(HttpServletRequest request){
        //解析token
        //一、获取token，解析token，获取用户id--
//        String token = request.getHeader("Authorization");
//        if (StringUtils.isBlank(token)){
//            throw new RuntimeException("非法token");
//        }
//        //解析token
//        Claims claims = JWTUtil.parseToken(token, "tanhua");
//        if (claims==null){
//            throw new RuntimeException("非法token");
//        }
//        Long userId = Long.valueOf(claims.get("userId").toString());


        UserInfoVo vo = new UserInfoVo();
        //调用userinfoservice
        UserInfo userInfo = userInfoService.findById(UserHolder.getUserId());

        if (userInfo!=null){
            BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性

            if(userInfo.getAge() != null) {
                vo.setAge(userInfo.getAge().toString());
            }
        }



        return ResponseEntity.ok(vo);
    }

    /**
     * 修改用户信息
     * @param userInfo
     * @param request
     * @return
     */
    @PutMapping
    public ResponseEntity updatUserInfo(@RequestBody UserInfo userInfo,HttpServletRequest request){
        //1 获取token，解析token，拿到userId
        //解析token
        //一、获取token，解析token，获取用户id--
//        String token = request.getHeader("Authorization");
//        if (StringUtils.isBlank(token)){
//            throw new RuntimeException("非法token");
//        }
//        //解析token
//        Claims claims = JWTUtil.parseToken(token, "tanhua");
//        if (claims==null){
//            throw new RuntimeException("非法token");
//        }
//        Long userId = Long.valueOf(claims.get("userId").toString());

        //2 远程调用更新用户信息
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }


    @GetMapping("/settings")
    public ResponseEntity<SettingsVo> settings(){
//        Long userId = UserHolder.getUserId();
        SettingsVo settingsVo = settingsService.settings();
        return ResponseEntity.ok(settingsVo);
    }



    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody QuestionVo questionVo){
        questionService.saveOrUpdate(questionVo);
        return ResponseEntity.ok(null);
    }


    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Settings settings){
        settingsService.saveOrUpdate(settings);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/blacklist")
    public ResponseEntity<BlackListVo> getBlackListByPage(@RequestParam(defaultValue = "1") Integer page,
                                                          @RequestParam(defaultValue = "10") Integer pagesize){
        BlackListVo vo = blackListService.getBlackListByPage(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid")Long blackUserId){
        blackListService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
}
